﻿using System;
using System.Windows.Media;
using System.Windows.Markup;

namespace Eniux.Framework.Extensions.MarkupExtensions
{
    /// <summary>
    /// ImageBrush标记扩展
    /// </summary>
    /// <example>
    /// <code>
    /// 
    ///   </code>
    /// </example>
    public class ImageBrushExtension : MarkupExtension
    {
        #region 私有变量

        /// <summary>
        /// 图片路径
        /// </summary>
        private  object path;

        #endregion

        #region 构造

        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="path"></param>
        public ImageBrushExtension(object path)
        {
            this.path = path;
        }

        #endregion

        #region 重写方法

        /// <summary>
        /// 返回值
        /// </summary>
        /// <param name="serviceProvider"></param>
        /// <returns></returns>
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            if (path.GetType() == typeof(ImageSource))
                return new ImageBrush((ImageSource)path);
            return new ImageBrush((ImageSource)new ImageSourceConverter().ConvertFromString("pack://siteOfOrigin:,,,/" + path));
        }

        #endregion
    }
}
